<?php
// +----------------------------------------------------------------------
// | title: 标题注释
// +----------------------------------------------------------------------
// | Author: TZ.Leong
// +----------------------------------------------------------------------
// | Date: 2019/7/3 11:54
// +----------------------------------------------------------------------

namespace app\single\model;


use think\exception\PDOException;
use think\Model;

class PayLogModel extends Model
{

    protected $table = "role_pay_log";

    public function initialize()
    {
    }

    public function addPayLog($data): bool
    {
        $this->field("order_id, cp_order_id, role_id, username, platform, server_id, rmb, charge_type, sub_type, gold, now_level, pay_date, pay_time")
            ->insert($data);
        try {
            $this->commit();
        } catch (PDOException $e) {
            return false;
        }
        return true;
    }

    /**
     * 统计充值金额，次数，人数。。。
     * @param array $payMap
     * @return mixed
     */
    public function statPay($payMap = array())
    {
        $data = $this->alias("pl")
            ->join([config("database.game") . ".role" => "r"], "r.id = pl.role_id")
            ->where($payMap)->group("pl.platform")
            ->column("pl.platform,SUM(pl.rmb) AS rmb,COUNT(pl.id) AS num,COUNT(DISTINCT pl.role_id) AS players");
        echo "debug somethings last sql :" . $this->getLastSql().PHP_EOL;
        return $data;
    }

    /**
     * 查询充值类型是否已经有记录
     * @param $roleId
     * @param $payType
     * @return bool
     */
    public function checkPayType($roleId, $payType)
    {
        $map["role_id"] = $roleId;
        $map["charge_type"] = $payType;
        $info = $this->where($map)->value("id");
        if ($info) {
            return true;
        }
        return false;
    }
}